/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipti.br.tag.entity;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.validation.constraints.Size;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Entity
public class Organization extends AbstractEntity{

    @Size(min=3, max=60, message="Inserir valor entre 3 e 60")
    @Column(nullable=false, length=60, unique=true)
    private String name;
    
    @Column(nullable=false, length=20)
    private String acronym;

    @ManyToOne
    @JoinColumn(name="father_id")
    private Organization org_father;
    
    @OneToMany(mappedBy = "organization")
    private List<Personage> personages;
    
    public Organization(){
    }

    @Override
    public void generateHash(){
        if(getId() == null){
            String hash = "" + this.getName()  + this.getAcronym()
                    + ((this.getOrg_father() != null) ? this.getOrg_father().getId() : null);
            this.setId(Long.valueOf(EncryptionUtil.encrypt(hash).hashCode()));
        }
    }

    public Organization(String name, String acronym, Organization org_father) {
        this.name = name;
        this.acronym = acronym;
        this.org_father = org_father;
    }

    public String getAcronym() {
        return acronym;
    }

    public void setAcronym(String acronym) {
        this.acronym = acronym;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Organization getOrg_father() {
        return org_father;
    }

    public void setOrg_father(Organization org_father) {
        this.org_father = org_father;
    }

    public List<Personage> getPersonages() {
        return personages;
    }

    public void setPersonages(List<Personage> personages) {
        this.personages = personages;
    }

    @Override
    public String toString() {
        return "Organization{" + "id=" + this.getId() + '}';
    }
}
